
package com.shop.cloud.mall.admin.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.shop.cloud.common.core.constant.CommonConstants;
import com.shop.cloud.common.core.util.R;
import com.shop.cloud.common.security.util.SecurityUtils;
import com.shop.cloud.mall.admin.mapper.OrderInfoMapper;
import com.shop.cloud.mall.admin.service.OrderInfoService;
import com.shop.cloud.mall.common.entity.OrderInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * 大屏统计
 *
 * @author
 * @date 2019-09-10 15:21:22
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/largescreen")
@Tag(description = "largescreen", name = "大屏统计")
public class LargeScreenController {

    private final OrderInfoService orderInfoService;
	private final OrderInfoMapper orderInfoMapper;

	/**
	 * 查询统计
	 * @param orderInfo
	 * @return R
	 */
	@Operation(summary = "查询统计")
	@GetMapping("/statistics")
//	@PreAuthorize("@ato.hasAuthority('mall:largescreen:statistics')")
	public R statistics(OrderInfo orderInfo){
		long count = orderInfoService.count(Wrappers.<OrderInfo>lambdaQuery()
				.eq(OrderInfo::getIsPay, CommonConstants.YES)
				.between(OrderInfo::getCreateTime, orderInfo.getBeginTime(), orderInfo.getEndTime())
		);
		orderInfo.setIsPay(CommonConstants.YES);
		if(StrUtil.isNotBlank(SecurityUtils.getUser().getShopId())){
			orderInfo.setShopId(SecurityUtils.getUser().getShopId());
		}
		BigDecimal sum = orderInfoService.sumPaymentPrice(orderInfo);
		Map<String, Object> rs = new HashMap<>();
		rs.put("count", count);
		rs.put("sum", sum);
		return R.ok(rs);
	}

	/**
	 * 订单数量分析
	 * @return R
	 */
	@Operation(summary = "订单数量分析")
	@GetMapping("/statistics/ordercount")
//	@PreAuthorize("@ato.hasAuthority('mall:largescreen:statistics')")
	public R statisticsOrderCount(OrderInfo orderInfo ){
		Map<String, Object> rs = new HashMap<>();
		rs.put("shopInfo", orderInfoMapper.statisticsOrderCountShopInfo(orderInfo));
		if(StrUtil.isNotBlank(SecurityUtils.getUser().getShopId())){
			orderInfo.setShopId(SecurityUtils.getUser().getShopId());
		}
		rs.put("spu", orderInfoMapper.statisticsOrderCountSpu(orderInfo));
		rs.put("category", orderInfoMapper.statisticsOrderCountCategory(orderInfo));
		return R.ok(rs);
	}

	/**
	 * 商品销量分析
	 * @return R
	 */
	@Operation(summary = "商品销量分析")
	@GetMapping("/statistics/spusale")
//	@PreAuthorize("@ato.hasAuthority('mall:largescreen:statistics')")
	public R statisticsSpuSale(OrderInfo orderInfo ){
		Map<String, Object> rs = new HashMap<>();
		rs.put("shopInfo", orderInfoMapper.statisticsSpuSaleShopInfo(orderInfo));
		if(StrUtil.isNotBlank(SecurityUtils.getUser().getShopId())){
			orderInfo.setShopId(SecurityUtils.getUser().getShopId());
		}
		rs.put("spu", orderInfoMapper.statisticsSpuSale(orderInfo));
		rs.put("category", orderInfoMapper.statisticsSpuSaleCategory(orderInfo));
		return R.ok(rs);
	}

	/**
	 * 销售金额分析
	 * @return R
	 */
	@Operation(summary = "销售金额分析")
	@GetMapping("/statistics/saleamount")
//	@PreAuthorize("@ato.hasAuthority('mall:largescreen:statistics')")
	public R statisticsSaleAmount(OrderInfo orderInfo ){
		Map<String, Object> rs = new HashMap<>();
		rs.put("shopInfo", orderInfoMapper.statisticsSaleAmountShopInfo(orderInfo));
		if(StrUtil.isNotBlank(SecurityUtils.getUser().getShopId())){
			orderInfo.setShopId(SecurityUtils.getUser().getShopId());
		}
		rs.put("spu", orderInfoMapper.statisticsSaleAmountSpu(orderInfo));
		rs.put("category", orderInfoMapper.statisticsSaleAmountCategory(orderInfo));
		return R.ok(rs);
	}
}
